package leetCode.fourthWeek;

public class Code_63 {
  public int uniquePathsWithObstacles(int[][] obstacleGrid) {
    int[][] tmp = new int[obstacleGrid.length][obstacleGrid[0].length];
    for (int i = 0; i < obstacleGrid[0].length; i++) { // 横向第一行上有一个1，将其&后面所有的位置置0
      if (obstacleGrid[0][i] == 1) {
        break;
      }
      tmp[0][i] = 1;
    }
    for (int i = 0; i < obstacleGrid.length; i++) { // 纵向第一列上有一个1，将其&后面所有的位置置0
      if (obstacleGrid[i][0] == 1) {
        break;
      }
      tmp[i][0] = 1;
    }
    for (int i = 1; i < obstacleGrid.length; i++) { // 将障碍物置为-1
      for (int j = 1; j < obstacleGrid[0].length; j++) {
        if (obstacleGrid[i][j] == 1) {
          tmp[i][j] = -1;
        }
      }
    }
    for (int i = 1; i < obstacleGrid.length; i++) {
      for (int j = 1; j < obstacleGrid[0].length; j++) {
        if (tmp[i][j] == -1) {
          tmp[i][j] = 0;
        } else {
          tmp[i][j] = tmp[i - 1][j] + tmp[i][j - 1];
        }
      }
    }
    return tmp[tmp.length - 1][tmp[0].length - 1];
  }
}
